﻿using System;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Data;

namespace Maticsoft.Common
{
    
    /// <summary>
    /// 页面控件状态的记录
    /// </summary>
    public class PageRecord
    {
        /// <summary>
        /// 初始化Dictionary
        /// </summary>
        /// <param name="dt1"></param>
        /// <returns></returns>
        public  Dictionary<string, bool> InitializeUsersDic(DataTable dt)
        {
            Dictionary<string, bool> currentDic = new Dictionary<string, bool>();
            //将DataTable中的每一行的第一列内容存储到Dictionary中
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                currentDic.Add(dt.Rows[i][0].ToString(), false);
            }
            return currentDic;
        }

        /// <summary>
        /// 记住在所有页面的每个复选框
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="gdv"></param>
        /// <param name="control">gridview控件的名字</param>

        public void RememberOldValues(Dictionary<string, bool> dic, GridView gdv, string control)
        {
            for (int i = 0; i < gdv.Rows.Count; i++)
            {
                string currentValue = gdv.Rows[i].Cells[1].Text.Trim();
                CheckBox currentCbx = gdv.Rows[i].Cells[0].FindControl(control) as CheckBox;
                if (currentCbx.Checked && dic[currentValue] == false)
                {
                    dic[currentValue] = true;
                }
                else if (!currentCbx.Checked && dic[currentValue] == true)
                {
                    dic[currentValue] = false;
                }
            }
        }

       
        /// <summary>
        /// 重新填充站在所有页面的每个复选框
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="gdv"></param>
        /// <param name="control">gridview控件的名字</param>
        public void RePopulateValues(Dictionary<string, bool> dic, GridView gdv, string control)
        {
            foreach (GridViewRow row in gdv.Rows)
            {
                string currentValue = row.Cells[1].Text.Trim();
                CheckBox currentCbx = row.Cells[0].FindControl(control) as CheckBox;
                if (dic[currentValue] == true)
                {
                    currentCbx.Checked = true;
                }
                else
                {
                    currentCbx.Checked = false;
                }
            }
        }

        
        /// <summary>
        /// 在所有页面选择所有复选框
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="gdv"></param>
        public void SelectAll(Dictionary<string, bool> dic)
        {
            string[] keys = dic.Keys.ToArray();
            dic.Clear();
            foreach (string key in keys)
            {
                dic.Add(key, true);
            }
        }
        
        /// <summary>
        /// 取消选择所有复选框在所有页面
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="gdv"></param>
        public void UnSelectAll(Dictionary<string, bool> dic)
        {
            string[] keys = dic.Keys.ToArray();
            dic.Clear();
            foreach (string key in keys)
            {
                dic.Add(key, false);
            }
        }

    }
}
